package com.hainatong.shiphelper.module.handlejob.fragment;

import java.util.ArrayList;
import java.util.List;

import com.base.common.utils.UIUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.handlejob.adapter.MyHandleInfoAdapter;
import com.hainatong.shiphelper.module.handlejob.entity.HandleInfoBean;
import com.hainatong.shiphelper.module.handlejob.viewmodel.HandleJobViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

public class MyHandleHistoryFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, HandleJobViewModel>
{
    private MyHandleInfoAdapter adapter;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        adapter = new MyHandleInfoAdapter();

        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager(getActivity()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.addItemDecoration( new ListItemDecoration(SizeUtils.dp2px( 10 )));
        viewDataBinding.recyclerview.setAdapter( adapter );


        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader(getContext()));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter(getContext()));
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {
            viewModel.tryRefreshHandle();
        } );
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
            viewModel.loadMoreHandle();
        } );

        adapter.setOnItemClickListener( (adapter1, view1, position) -> {

           //startActivity( new Intent( getActivity(), HandleJobDetailActivity.class ));

           HandleInfoBean tempHandle = adapter.getItem( position );
           ComFragmentActivity.startViewByParam( getActivity() , Constants.VIEW_HANDLE_TASK_DETAIL , GsonUtil.obj2json( tempHandle ));

        } );

    }

    @Override
    protected void lazyLoad()
    {
        List<HandleInfoBean> handleInfoList  = new ArrayList<>();
        HandleInfoBean infoBean = new HandleInfoBean();

        handleInfoList.add(infoBean);
        adapter.setList(handleInfoList);
        adapter.notifyDataSetChanged();

        if(UIUtils.isNotEmpty(GlobalConfig.getInstance().getUserInfo()))
        {
            viewModel.queryHandleHistoryByUser( GlobalConfig.getInstance().getUserInfo().getUuid() );
        }
        // 查询到的历史
        viewModel.getHandleList().observe( this ,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        } );
    }
}
